<?php
/********************
 *@param function:產品管理 model
 *@param datetime 2011-6-7
 *@param author solgh
 ********************/
class Products_mdl extends CI_model{
  //產品.
  const TAB_NAME='clickrcms_products';
  const TAB_DESC='clickrcms_products_description';
  const TAB_IMAGE='clickrcms_products_image';
  const TAB_UNIT='clickrcms_products_units';
  const TAB_PRICE='clickrcms_products_itemprice';
  //類別
  const TAB_CATE='clickrcms_categorys';
  const TAB_CATEG='clickrcms_categorys_description';
  public $insert_id = 0;
  function __construct(){
    parent::__construct();
    log_message('debug','products_mdl Model class Initialized');
  }

  /****
   *查詢所products,加分页
   *$data :查询参数.
   */
 public function get_products($data=array()){
   if(!empty($data)){
     extract($data);
     isset($field) && isset($sort)?$this->db->order_by($field.' '.$sort):$this->db->order_by('n.sort_order ASC');
     isset($limits) && isset($offset)?$this->db->limit($limits,$offset):'';
     isset($status) && !empty($status)?$this->db->where('n.status',$status):'';
     isset($language_id) && !empty($language_id)?$this->db->where('nd.language_id',$language_id):'';
     isset($language_id) && !empty($language_id)?$this->db->where('(ncd.language_id =  '.$language_id.' OR ncd.language_id is null)'):'';
     isset($no_id) && !empty($no_id)?$this->db->where_not_in('n.id',$no_id):'';
     isset($title) && !empty($title)?$this->db->like(array('nd.title',$title)):'';
     isset($number) && !empty($number)?$this->db->or_like('nd.number',$number):'';
     isset($introduction) && !empty($introduction)?$this->db->like('nd.introduction',$introduction):'';
     isset($category_id) && !empty($category_id)?$this->db->where_in('n.category_id',$category_id):'';
     isset($product_ids) && is_array($product_ids) ? $this->db->where_in('n.id',$product_ids):'';
     isset($type) && !empty($type)?$this->db->where('n.type',$type):'';

     $this->db->group_by('n.id');
     $this->db->select('n.*,nd.*,ncd.title as ncd_title,nun.name as unit');
     $this->db->from(self::TAB_NAME.' AS n');
     $this->db->join(self::TAB_DESC.' AS nd', 'n.id = nd.product_id','LEFT');
     $this->db->join(self::TAB_UNIT.' AS nun', 'n.product_unit_id = nun.id','LEFT');
     //類別
     $this->db->join(self::TAB_CATE.' AS nc', 'n.category_id = nc.id','LEFT');
     $this->db->join(self::TAB_CATEG.' AS ncd', 'nc.id = ncd.category_id','LEFT');
     return $this->db->get();
   }
 }
 
  /****
   * 統計所有的products
   */
  public function totle_products($data=array()){
    if(!empty($data)){
      extract($data);
      isset($status) && !empty($status)?$this->db->where('n.status',$status):'';
      isset($language_id) && !empty($language_id)?$this->db->where('nd.language_id',$language_id):'';
      isset($no_id) && !empty($no_id)?$this->db->where('n.id !=',$no_id):'';
      isset($title) && !empty($title)?$this->db->like('nd.title',$title):'';
      isset($introduction) && !empty($introduction)?$this->db->like('nd.introduction',$introduction):'';
      isset($category_id) && !empty($category_id)?$this->db->where_in('n.category_id',$category_id):'';
      isset($type) && !empty($type)?$this->db->where('n.type',$type):'';
      $this->db->select('n.*,nd.title');
      $this->db->from(self::TAB_NAME.' AS n');
      $this->db->join(self::TAB_DESC.' AS nd', 'n.id = nd.product_id','LEFT');
      return $this->db->count_all_results();
    }else{
      return $this->db->count_all(self::TAB_NAME);
    }
  }
  /*****
   * 根據key來刪除
   */
  public function delete_products($delete_id=''){
    if(!empty($delete_id)){
      // $this->db->where("category_id",$delete_id);
      // $this->db->delete(self::TAB_IMAGE);
      $this->db->where("product_id",$delete_id);
      $this->db->delete(self::TAB_DESC);
      $this->db->where("id",$delete_id);
      $this->db->delete(self::TAB_NAME);
      $this->delItemPrice($delete_id);
      return ($this->db->affected_rows()>0)?true:false;
    }
  }
  /*****
   * 根據ID數組來刪除 批量刪除
   */
  public function delete_batch($batch=array()){
    if(!empty($batch)){
      // $this->db->where_in("category_id",$batch);
      // $this->db->delete(self::TAB_IMAGE);
      $this->db->where_in("product_id",$batch);
      $this->db->delete(self::TAB_DESC);
      $this->db->where_in("id",$batch);
      $this->db->delete(self::TAB_NAME);
      return ($this->db->affected_rows()>0)?true:false;
    }
  }
  /****
   *根据ID来查询得到一个信息.
   *$id :要查詢的ID
   */
  public function get_products_by_id($id=''){
   if(!empty($id)){
      $data=array();
      $this->db->where('id',$id);
      $get_result=$this->db->get(self::TAB_NAME);
      if($get_result->num_rows()>0)
      {
        $data=$get_result->row_array();
      }
      $get_result->free_result();
      return $data;
    }
  }

  /***
   * 修改一個products
   * @param array $data
   * @param int  $c_id
   * return :受影响的行数
   */
  public function edit_products($data=array(),$b_id=0){
    if(!empty($data) && !empty($b_id)){
      extract($data);
      $this->db->where('id',$b_id);
      $this->db->update(self::TAB_NAME,$general);
      //描述.
      if(isset($descriptions) && is_array($descriptions)){
        $this->db->where('product_id',$b_id);
        $this->db->delete(self::TAB_DESC);
        foreach ($descriptions as $language_id => $value) {
          $value['product_id']=$b_id;
          $value['language_id']=$language_id;
          $this->db->insert(self::TAB_DESC,$value);
        } //end foreach;
      }
      return ($this->db->affected_rows()>0)?true:false;
    }
  }
  /***
   * 新增一個 products
   */
  public function add_products($data=array()){
    if(!empty($data)){
      extract($data);
      $this->db->insert(self::TAB_NAME,$general);
      $this->insert_id = $insert_id=$this->db->insert_id(); //獲取新增id
      if($insert_id>0){
     
        if(isset($descriptions) && is_array($descriptions)){
          foreach ($descriptions as $language_id => $value) {
            $value['product_id']=$insert_id;
            $value['language_id']=$language_id;
            $this->db->insert(self::TAB_DESC,$value);
          } //end foreach;
        }
      }//end if;
      return ($this->db->affected_rows()>0)?true:false;
    }
  }

  //根據id 獲取 多語言的descriptions
  public function get_descriptions($id=0,$lang){
    if(!empty($id)){
        $this->db->where('product_id',$id);
        $this->db->where_in('language_id',$lang);
        return $this->db->get(self::TAB_DESC);
    }
  }
   //(前臺) 更新瀏覽次數
  public function add_view_num($id=''){
    if(!empty($id)){
      $this->db->where('id',$id);
      $this->db->set('view_num', 'view_num+1', FALSE);
      $this->db->update(self::TAB_NAME);
    }
  }


  //--Banner廣告--//
  //根據id 獲取 多圖片
  public function get_images($data=array()){
    if(!empty($data)){
        extract($data);
        $this->db->order_by('sort_order ASC');
        $this->db->where('category_id',$album_id);
        isset($limits) && isset($offset)?$this->db->limit($limits,$offset):'';
        return $this->db->get(self::TAB_IMAGE);
    }
  }
  
    //根據id 獲取 多圖片
  public function totle_images($data=array()){
    if(!empty($data)){
      extract($data); 
      $this->db->order_by('sort_order ASC');
      $this->db->where('category_id',$album_id);
      return $this->db->count_all_results(self::TAB_IMAGE);
    }else{
      return $this->db->count_all(self::TAB_IMAGE);
    }
  }
  //添加相簿图片
  public function add_images($data=array()){
    if(!empty($data)){
        $this->db->insert(self::TAB_IMAGE,$data);
        return ($this->db->affected_rows()>0)?true:false;
    }
  }
  //修改相簿圖片
  public function edit_images($data=array(),$image_id=0){
    if(!empty($data) && !empty($image_id)){
      $this->db->where('id',$image_id);
      $this->db->update(self::TAB_IMAGE,$data);
      return ($this->db->affected_rows()>0)?true:false;
    }
  }
  //刪除相簿圖片
  public function delete_image($id=0){
    if(!empty($id)){
        $this->db->where('id',$id);
        $this->db->delete(self::TAB_IMAGE);
        return ($this->db->affected_rows()>0)?true:false;
    }
  }


  //(前臺) 根據unique_url查詢.
  public function get_products_by_unique_url($data=array()){
    if(!empty($data)){
      extract($data);
      isset($status) && !empty($status)?$this->db->where('n.status',$status):'';
      isset($type) && !empty($type)?$this->db->where('n.type',$type):'';
      isset($language_id) && !empty($language_id)?$this->db->where('nd.language_id',$language_id):'';
      isset($unique_url) && !empty($unique_url)?$this->db->where('n.unique_url',$unique_url):'';
      isset($id) && !empty($id)?$this->db->where('n.id',$id):'';
      $this->db->select('n.*,nd.*,nun.name as unit');
      $this->db->from(self::TAB_NAME.' AS n');
      $this->db->join(self::TAB_DESC.' AS nd', 'n.id = nd.product_id','LEFT');
      $this->db->join(self::TAB_UNIT.' AS nun', 'n.product_unit_id = nun.id','LEFT');
      $query=$this->db->get();
      $return=array();
      if($query->num_rows()>0)
      {
        $return=$query->row_array();
      }
      $query->free_result();
      return $return;
    }
  }

  //(前臺)獲取上一個 下一個
  public function get_fist_last($data=array()){
    if(!empty($data)){
      extract($data);
      isset($status) && !empty($status)?$this->db->where('status',$status):'';
      isset($type) && !empty($type)?$this->db->where('type',$type):'';
      isset($category_id) && !empty($category_id)?$this->db->where('category_id',$category_id):'';
      isset($language_id) && !empty($language_id)?$this->db->where('nd.language_id',$language_id):'';
      if($direction=='last'){ //下一個
        $this->db->where('add_time >',$id);
        $this->db->order_by('add_time ASC');
      }else{  //上一個
        $this->db->where('add_time <',$id);
        $this->db->order_by('add_time DESC');
      }
      $this->db->limit(1);
      $this->db->select('n.*,nd.title');
      $this->db->from(self::TAB_NAME.' AS n');
      $this->db->join(self::TAB_DESC.' AS nd', 'n.id = nd.product_id','LEFT');
      $query=$this->db->get();
      $return=array();
      if($query->num_rows()>0)
      {
        $return=$query->row_array();
      }
      $query->free_result();
      return $return;
    }
  }
  public function delItemPrice($product_id){
    if(is_array($product_id)){
      $this->db->where_in("product_id",$product_id);
    }else{
      $this->db->where('product_id',$product_id);
    }
      
      $this->db->delete(self::TAB_PRICE);
      return ($this->db->affected_rows()>0)?true:false;
  }
  public function addItemPrice($data,$product_id){
      $this->delItemPrice($product_id);
      $this->db->insert_batch(self::TAB_PRICE,$data);
      return ($this->db->affected_rows()>0)?true:false;
  }
  public function getItemPrice($product_id){
      $this->db->where('product_id',$product_id);
      $items = $this->db->get(self::TAB_PRICE)->result_array();
      if($items){
        $base = array();
         foreach ($items as $key => $value) {
             $base[$value['spec_key']] = $value;
         }
         return $base;
      }
      return null;
  }
  public function getOneItemPrice($product_id,$spec_key){
      $this->db->where('product_id',$product_id);
      $this->db->where('spec_key',$spec_key);
      $item = $this->db->get(self::TAB_PRICE)->row_array();
      
      return $item;
  }
  public function getItemPriceSelectd($product_id){
       $data = $this->getItemPrice($product_id);
       if($data){
          $base = array();
          foreach($data as $key=>$vo){
              $base = array_merge($base,explode("_", $key));
          }
          return $base;
       }
       return array();
  }
  public function getProductItemPrice($items){
      if($items){
          $itemIDs = array_keys($items);
          $itemIDs = implode("_", $itemIDs);
          $itemIDs = explode("_", $itemIDs);
          $this->db->where_in('id',$itemIDs);
          $items = $this->db->get('clickrcms_specs_items')->result_array();
          if($items){
             $base = array();
             foreach ($items as $item) {
                $base[$item['spec_id']][] = $item;
             }
             return $base;
          }
      }
      return null;
  }
}